package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IPayOrderService;
import com.mdd.admin.validate.PayOrderCreateValidate;
import com.mdd.admin.validate.PayOrderUpdateValidate;
import com.mdd.admin.validate.PayOrderSearchValidate;
import com.mdd.admin.vo.PayOrderListedVo;
import com.mdd.admin.vo.PayOrderDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.PayOrder;
import com.mdd.common.mapper.PayOrderMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 下单订单(目前只有快递)实现类
 * @author DEREK
 */
@Service
public class PayOrderServiceImpl implements IPayOrderService {
        
    @Resource
    PayOrderMapper payOrderMapper;

    /**
     * 下单订单(目前只有快递)列表
     *
     * @author DEREK
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<PayOrderListedVo>
     */
    @Override
    public PageResult<PayOrderListedVo> list(PageValidate pageValidate, PayOrderSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<PayOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        payOrderMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:userId@user_id:int",
            "=:relatedId@related_id:int",
            "=:orderSn@order_sn:str",
            "=:orderType@order_type:int",
            "=:kuaidiNum@kuaidi_num:str",
            "=:payStatus@pay_status:int",
            "=:payType@pay_type:int",
            "=:orderAmount@order_amount:str",
            "=:actualAmount@actual_amount:str",
            "=:tradeNo@trade_no:str",
            "=:remark:str",
            "=:extend:str",
        });

        IPage<PayOrder> iPage = payOrderMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<PayOrderListedVo> list = new LinkedList<>();
        for(PayOrder item : iPage.getRecords()) {
            PayOrderListedVo vo = new PayOrderListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 下单订单(目前只有快递)详情
     *
     * @author DEREK
     * @param id 主键参数
     * @return PayOrder
     */
    @Override
    public PayOrderDetailVo detail(Integer id) {
        PayOrder model = payOrderMapper.selectOne(
                new QueryWrapper<PayOrder>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        PayOrderDetailVo vo = new PayOrderDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 下单订单(目前只有快递)新增
     *
     * @author DEREK
     * @param createValidate 参数
     */
    @Override
    public void add(PayOrderCreateValidate createValidate) {
        PayOrder model = new PayOrder();
        model.setUserId(createValidate.getUserId());
        model.setRelatedId(createValidate.getRelatedId());
        model.setOrderSn(createValidate.getOrderSn());
        model.setOrderType(createValidate.getOrderType());
        model.setKuaidiNum(createValidate.getKuaidiNum());
        model.setPayStatus(createValidate.getPayStatus());
        model.setPayType(createValidate.getPayType());
        model.setOrderAmount(createValidate.getOrderAmount());
        model.setActualAmount(createValidate.getActualAmount());
        model.setTradeNo(createValidate.getTradeNo());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        model.setRemark(createValidate.getRemark());
        model.setExtend(createValidate.getExtend());
        payOrderMapper.insert(model);
    }

    /**
     * 下单订单(目前只有快递)编辑
     *
     * @author DEREK
     * @param updateValidate 参数
     */
    @Override
    public void edit(PayOrderUpdateValidate updateValidate) {
        PayOrder model = payOrderMapper.selectOne(
                new QueryWrapper<PayOrder>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setUserId(updateValidate.getUserId());
        model.setRelatedId(updateValidate.getRelatedId());
        model.setOrderSn(updateValidate.getOrderSn());
        model.setOrderType(updateValidate.getOrderType());
        model.setKuaidiNum(updateValidate.getKuaidiNum());
        model.setPayStatus(updateValidate.getPayStatus());
        model.setPayType(updateValidate.getPayType());
        model.setOrderAmount(updateValidate.getOrderAmount());
        model.setActualAmount(updateValidate.getActualAmount());
        model.setTradeNo(updateValidate.getTradeNo());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        model.setRemark(updateValidate.getRemark());
        model.setExtend(updateValidate.getExtend());
        payOrderMapper.updateById(model);
    }

    /**
     * 下单订单(目前只有快递)删除
     *
     * @author DEREK
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        PayOrder model = payOrderMapper.selectOne(
                new QueryWrapper<PayOrder>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        payOrderMapper.delete(new QueryWrapper<PayOrder>().eq("id", id));
    }

}
